
 using System;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Data;
 using System.Drawing;
 using System.Text;
 using System.Windows.Forms;
using LPV_POS;
public partial class frmDM_TIENGIO1 : Form
{
    public string ma = "";
    DataTable tbMainDS = new DataTable();
    DataTable tbMain = new DataTable();
    Boolean iloadform = false;

    #region"    frmDM_TIENGIO1"
    public frmDM_TIENGIO1()
    {
        InitializeComponent();
    }
    #endregion

    #region"    frmDM_TIENGIO1_Load"
    public void frmDM_TIENGIO1_Load(object sender, EventArgs e)
    {
        btnluu.Enabled = false;
        btnxoa.Enabled = false;
        this.showcombobox();
        this.showdataDS();
        iloadform = true;


    }
    #endregion

    #region"    các hàm load dữ liệu"
    #region"    showdata"
    void showdata()
    {

        thuvien othuvien = new thuvien();//lớp truy xuat du lieu 
        tbMain = othuvien.SP_SelectDataTable("sp_DM_TIENGIO1",
                           new string[] { "@flag", "@OBJID" },
                           new object[] { 4, ma });

    }
    #endregion
    #region"    showdataDS"
    void showdataDS()
    {
        string[] param = new string[3];
        object[] value = new object[3];
        
        param[0] = "@flag";
        value[0] = 4;

        param[1] = "@MA_LOAIDICHVU";
        value[1] = cboMA_LOAIDICHVU.SelectedValue;

        param[2] = "@DM_BAN_LOAI_MA";
        value[2] = cboDM_BAN_LOAI_MA.SelectedValue;

        grdDM.AutoGenerateColumns = false;
        thuvien othuvien = new thuvien();//lớp truy xuat du lieu
        tbMainDS = othuvien.SP_SelectDataTable("sp_DM_TIENGIO1", param, value);
        grdDM.DataSource = tbMainDS;
        grdDM.Refresh();

    }
    #endregion
    #region"    showinfo"
    void showinfo()
    {
        this.showdata();
        if (tbMain.Rows.Count == 0)
        {
            //cboMA_LOAIDICHVU.SelectedIndex = -1;
            dtpTU_GIO.Value = DateTime.Now;
            dtpTU_GIO.Checked = false;
            dtpDEN_GIO.Value = DateTime.Now;
            dtpDEN_GIO.Checked = false;
            numT1.Value = 0;
            numT2.Value = 0;
            numT3.Value = 0;
            numT4.Value = 0;
            numT5.Value = 0;
            numT6.Value = 0;
            numT7.Value = 0;
            chk_tinh_USD.Checked = false;
            return;
        }
        cboMA_LOAIDICHVU.SelectedValue = tbMain.Rows[0]["MA_LOAIDICHVU"];

        dtpTU_GIO.Text = tbMain.Rows[0]["TU_GIO"] + "";

        dtpDEN_GIO.Text = tbMain.Rows[0]["DEN_GIO"] + "";
        numT1.Value = decimal.Parse(string.Concat("0" + tbMain.Rows[0]["T1"]).Replace("0-", "-"));
        numT2.Value = decimal.Parse(string.Concat("0" + tbMain.Rows[0]["T2"]).Replace("0-", "-"));
        numT3.Value = decimal.Parse(string.Concat("0" + tbMain.Rows[0]["T3"]).Replace("0-", "-"));
        numT4.Value = decimal.Parse(string.Concat("0" + tbMain.Rows[0]["T4"]).Replace("0-", "-"));
        numT5.Value = decimal.Parse(string.Concat("0" + tbMain.Rows[0]["T5"]).Replace("0-", "-"));
        numT6.Value = decimal.Parse(string.Concat("0" + tbMain.Rows[0]["T6"]).Replace("0-", "-"));
        numT7.Value = decimal.Parse(string.Concat("0" + tbMain.Rows[0]["T7"]).Replace("0-", "-"));
        chk_tinh_USD.Checked = tbMain.Rows[0]["TINH_GIA_USD"] + "" == "True";
    }
    #endregion
    #region"    showcombobox"
    void showcombobox()
    {

        thuvien othuvien = new thuvien();
        DataTable tbMA_LOAIDICHVU = othuvien.SP_SelectDataTable("sp_DM_TIENGIO1", new string[] { "@flag" }, new object[] { 5 });
        cboMA_LOAIDICHVU.DataSource = tbMA_LOAIDICHVU;

        DataTable tbDM_BAN_LOAI = othuvien.SP_SelectDataTable("sp_DM_TIENGIO1", new string[] { "@flag" }, new object[] { 7 });
        cboDM_BAN_LOAI_MA.DataSource = tbDM_BAN_LOAI;
        if (tbDM_BAN_LOAI.Rows.Count > 0)
            cboDM_BAN_LOAI_MA.SelectedIndex = 0;

    }
    #endregion
    #endregion

    #region"    các sự kiện _Click của các nút THÊM - SỬA- TÌM KIẾM - THOÁT - tbpDanhSach"
    #region"    btnDSthem_Click"
    private void btnDSthem_Click(object sender, EventArgs e)
    {
        tbMain = tbMainDS.Copy();
        tbMain.Clear();
        ma = "-1";
        this.showinfo();
        this.setStatusButton(1);
    }

    #endregion
    #region"    btnDSthoat_Click"
    private void btnDSthoat_Click(object sender, EventArgs e)
    {
        //if (MessageBox.Show("Bạn muốn thoát?", "Thông báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1) == DialogResult.OK)
        {
            this.Dispose();
        }

    }
    #endregion

    #endregion

    #region"    các sự kiện _Click của các nút LƯU - XÓA - SỬA  "
    #region"    btnluu_Click"
    private void btnluu_Click(object sender, EventArgs e)
    {
        if (this.save() > 0)
        {
            MessageBox.Show("Lưu thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            this.showdataDS();
        }
    }

    #endregion
    #region"    save"
    int save()
    {
        int _iReasult = -1;
        if (this.ValidateSave())
        {
            thuvien othuvien = new thuvien();//lớp truy xuat du lieu
            _iReasult = othuvien.SP_Update("sp_DM_TIENGIO1",
                 new string[] { "@flag", "@OBJID", "@MA_LOAIDICHVU", "@TU_GIO", "@DEN_GIO", 
                                "@T1", "@T2", "@T3", "@T4", "@T5", "@T6", "@T7",
                                "@DM_BAN_LOAI_MA", "@TINH_GIA_USD"
                 },
                 new object[] { 1, ma, cboMA_LOAIDICHVU.SelectedValue, dtpTU_GIO.Value, dtpDEN_GIO.Value, 
                                numT1.Value, numT2.Value, numT3.Value, numT4.Value, numT5.Value, numT6.Value, numT7.Value ,
                                cboDM_BAN_LOAI_MA.SelectedValue,chk_tinh_USD.Checked
                 });
        }
        return _iReasult;
    }

    #endregion
    
    #region"    ValidateSave"
    Boolean ValidateSave()
    {
        return true;
    }

    #endregion
    
    #region"    delete"
    int delete()
    {
        thuvien othuvien = new thuvien();//lớp truy xuat du lieu    
        int _iReasult = othuvien.SP_Update("sp_DM_TIENGIO1",
                                 new string[] { "@flag", "@OBJID" },
                                 new object[] { 3, ma, });
        return _iReasult;
    }

    #endregion
    
    #region"    btnxoa_Click"
    private void btnxoa_Click(object sender, EventArgs e)
    {
        if (MessageBox.Show("Bạn muốn xóa?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
        {
            if (this.delete() > 0)
            {
                MessageBox.Show("Xóa thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                ma = "-1";
                this.showdataDS();
                this.setStatusButton(4);
            }
        }
    }

    #endregion

    #region"    sự kiên chọn trên lưới -- tblDanhSach"
    
    #region"    grdDM_TIENGIO1_SelectionChanged"
    private void grdDM_TIENGIO1_SelectionChanged(object sender, EventArgs e)
    {

        if (iloadform && grdDM.CurrentRow != null)
        {
            if (grdDM.CurrentRow.Index >= 0)
            {
                iloadform = false;
                ma = "-1";
                string _iSTT = grdDM.Rows[grdDM.CurrentRow.Index].Cells[0].Value + "";
                DataRow[] _dr = tbMainDS.Select("STT='" + _iSTT + "'");
                if (_dr.Length > 0)
                {
                    ma = _dr[0]["OBJID"] + "";
                    this.setStatusButton(2);
                }
                this.showinfo();
                iloadform = true;
            }
        }
    }
    private void grdDM_TIENGIO1_DoubleClick(object sender, EventArgs e)
    {
        this.grdDM_TIENGIO1_SelectionChanged(null, null);
    }
    #endregion
    #endregion
    #region "    setStatusButton"
    void setStatusButton(int i1New2Edt3Sch4Del5Sav)
    {
        btnDSthem.Enabled = i1New2Edt3Sch4Del5Sav != 1;
        btnxoa.Enabled = (i1New2Edt3Sch4Del5Sav != 1 && i1New2Edt3Sch4Del5Sav != 4 && i1New2Edt3Sch4Del5Sav != 3);

        btnluu.Enabled = i1New2Edt3Sch4Del5Sav != 5;
    }
    #endregion

    #region "  cbo_SelectionChanged "
    private void cbo_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    #endregion
    #region"    grdDM_TIENGIO1_DataError"
    private void grdDM_TIENGIO1_DataError(object sender, DataGridViewDataErrorEventArgs e)
    {
        string sError = e.Exception.Message;
    }
    #endregion
    #region"    grd_DataError"
    private void grd_DataError(object sender, DataGridViewDataErrorEventArgs e)
    {
        string sError = e.Exception.Message;
    }
    #endregion

    private void cboMA_LOAIDICHVU_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (iloadform)
        {
            iloadform = false;
            this.showdataDS();
            iloadform = true;
        }
    }
    #endregion

    private void lblT2_Click(object sender, EventArgs e)
    {
        numT1.Value = numT2.Value;
        numT3.Value = numT2.Value;
        numT4.Value = numT2.Value;
        numT5.Value = numT2.Value;
        numT6.Value = numT2.Value;
        numT7.Value = numT2.Value;
    }

    private void numT2_Leave(object sender, EventArgs e)
    {
        if (iloadform)
        {
            if (numT3.Value == 0)
                numT3.Value = numT2.Value;

            if (numT4.Value == 0)
                numT4.Value = numT2.Value;

            if (numT5.Value == 0)
                numT5.Value = numT2.Value;

            if (numT6.Value == 0)
                numT6.Value = numT2.Value;

            if (numT7.Value == 0)
                numT7.Value = numT2.Value;

            if (numT1.Value == 0)
                numT1.Value = numT2.Value;
        }
    }

    private void cboDM_BAN_LOAI_MA_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (iloadform)
            this.showdataDS();
    }

    private void frmDM_TIENGIO1_FormClosed(object sender, FormClosedEventArgs e)
    {
        foreach (Form frm in this.MdiParent.MdiChildren)
            frm.WindowState = FormWindowState.Maximized;
    }
}

